package com.chenxin.wms.process.inbound.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.enums.wms.IbDetailStatus;
import com.chenxin.wms.domain.IbDetailEntity;
import com.chenxin.wms.domain.IbMasterEntity;
import com.chenxin.wms.mapper.IbDetailMapper;
import com.chenxin.wms.mapper.IbMasterMapper;
import com.chenxin.wms.process.inbound.IProcIbMasterStatusUpdate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProcIbMasterStatusUpdate implements IProcIbMasterStatusUpdate {

    @Autowired
    private IbMasterMapper ibMasterMapper;

    @Autowired
    private IbDetailMapper ibDetailMapper;

    @Override
    public void statusUpdateByMasterId(Long ibMasterId, Long updater) {
        // 1.根据主单ID获取对应主单信息
        IbMasterEntity ibMaster = ibMasterMapper.selectById(ibMasterId);
        if (ibMaster == null) {
            return;
        }
        int ibMasterStatus;
        // 2.根据主单ID获取对应明细单信息
        QueryWrapper<IbDetailEntity> detailsQW = new QueryWrapper<>();
        detailsQW.eq("ib_master_id", ibMasterId);

        List<IbDetailEntity> ibDetails = ibDetailMapper.selectList(detailsQW);
        if (ibDetails == null || ibDetails.size() == 0) {
            return;
        }
        // 获取最大状态值与最小状态值
        int maxStatus = ibDetails.stream().mapToInt(IbDetailEntity::getIbDetailStatus).max().orElse(0);
        int minStatus = ibDetails.stream().mapToInt(IbDetailEntity::getIbDetailStatus).min().orElse(0);
        // 明细状态：0-初始状态 1-收货中 2-收货完成 3-待入库 4-取消 9-异常
        long count0 = ibDetails.stream().filter(detail -> detail.getIbDetailStatus() == IbDetailStatus.INIT.getCode())
                .count();
        long count1 = ibDetails.stream().filter(detail -> detail.getIbDetailStatus() == IbDetailStatus.EXEC.getCode())
                .count();
        long count2 = ibDetails.stream().filter(detail -> detail.getIbDetailStatus() == IbDetailStatus.DONE.getCode())
                .count();
        long count4 = ibDetails.stream().filter(detail -> detail.getIbDetailStatus() == IbDetailStatus.CANCEL.getCode())
                .count();
        long count9 = ibDetails.stream().filter(detail -> detail.getIbDetailStatus() == IbDetailStatus.ERROR.getCode())
                .count();
        if (count0 > 0L && count1 == 0L && count2 == 0L) {
            // 状态=0的情况：全部为初始状态,无执行中或收货完成状态
            ibMasterStatus = IbDetailStatus.INIT.getCode();
        } else if (minStatus == IbDetailStatus.DONE.getCode()) {
            // 状态=2的情况：全部为收货完成（最小值=2）
            ibMasterStatus = IbDetailStatus.DONE.getCode();
        } else if (minStatus == IbDetailStatus.CANCEL.getCode()) {
            // 状态=4的情况：全部为取消（最小值=4）
            ibMasterStatus = IbDetailStatus.CANCEL.getCode();
        } else if (minStatus == IbDetailStatus.ERROR.getCode()) {
            ibMasterStatus = IbDetailStatus.ERROR.getCode();
        } else {
            // 其他情况都为1
            ibMasterStatus = IbDetailStatus.EXEC.getCode();
        }

        Date workDate = new Date();
        // 3.将主单状态与明细单最小状态进行同步
        ibMaster.setIbMasterStatus(ibMasterStatus);
        ibMaster.setUpdater(updater);
        ibMaster.setUpdateDate(workDate);

        ibMasterMapper.updateById(ibMaster);

        // TODO 根据当前主单状态判断是否需要上报对应系统
        if (ibMasterStatus == IbDetailStatus.DONE.getCode()) {
            log.info("收货完成,上报SAP系统");
        }
    }
}
